package com.video.tiktokdownloader.nowatermark

import android.app.Activity
import android.content.ActivityNotFoundException
import android.content.Context
import android.content.Intent
import android.content.pm.PackageManager
import android.net.Uri
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.text.Html
import android.text.TextUtils
import kotlinx.android.synthetic.main.activity_setting.*

class SettingActivity :BaseActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_setting)

        toolbar_back.setOnClickListener {
            finish()
        }

        // https://668-judan.github.io/Tiktok-video/
        ll_setting_feedback.setOnClickListener {
            startEmail(this, "Feedback", "", "judansuper1@gmail.com")
        }
        ll_setting_privacy.setOnClickListener {
            try {
                viewUrl(this, "https://668-judan.github.io/Tiktok-video/")
            } catch (e: Exception) {
            }
        }
    }
}

fun startEmail(
    ctx: Context, emailSubject: String,
    emailBody: String? = "  ", emailTo: String?
) {
    val email = Intent(Intent.ACTION_SEND)
    if (emailBody != null && emailBody != "") {
        email.type = "text/html"
    } else {
        email.type = "application/octet-stream"
    }
    if (emailTo != null && emailTo != "") {
        val emailReciver = arrayOf(emailTo)
        // set default email address
        email.putExtra(Intent.EXTRA_EMAIL, emailReciver)
    }
    // set default mail subject
    email.putExtra(Intent.EXTRA_SUBJECT, emailSubject)
    // set default mail body
    email.putExtra(
        Intent.EXTRA_TEXT,
        Html.fromHtml(emailBody)
    )
    // start send email
    ctx.startActivity(
        Intent.createChooser(
            email,
            "Please choose you client to send!"
        ).addFlags(
            Intent.FLAG_ACTIVITY_NEW_TASK
        )
    )
}

fun viewUrl(
    activity: Activity,
    url: String,
    packageName: String? = null,
    mimeType: String? = null
): Boolean {
    val intent = Intent(Intent.ACTION_VIEW)

    if (!TextUtils.isEmpty(url)) {
        val uri = Uri.parse(url)

        if (TextUtils.isEmpty(mimeType)) {
            intent.data = uri
        } else {
            intent.setDataAndType(uri, mimeType)
        }
    }

    if (!TextUtils.isEmpty(packageName)) {
        intent.setPackage(packageName)
    }
    return if (activity.packageManager.resolveActivity(
            intent,
            PackageManager.MATCH_DEFAULT_ONLY
        ) != null
    ) {
        try {
            activity.startActivity(intent)
            true
        } catch (e: ActivityNotFoundException) {
            false
        }

    } else {
        false
    }
}

fun openTKUser(activity: Activity, uniqueId: String): Boolean {
    val url = "https://www.tiktok.com/@$uniqueId/"
//    var a = viewUrl(activity, url, "com.ss.android.ugc.trill")
//    if (!a) {
       val a = viewUrl(activity, url, "com.zhiliaoapp.musically")
//    }
    return a
}

fun openTK(activity: Activity) {
    try {
        val  intent = activity.packageManager.getLaunchIntentForPackage("com.zhiliaoapp.musically")
        if (intent == null) {
            viewUrl(activity, "https://www.tiktok.com/")
        } else {
            activity.startActivity(intent)
        }
    } catch (e: Exception) {
        viewUrl(activity, "https://www.tiktok.com/trending/")
    }
}